package com.hzy.demo.controllers;

import com.hzy.demo.DataService.DataServices;
import com.hzy.demo.models.RegionStats;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

@Controller
//@RequiredArgsConstructor
public class HomeController {

    private final DataServices dataServices;

    public HomeController(DataServices dataServices) {
        this.dataServices = dataServices;
    }


    @GetMapping("/")
    public String home(Model model, String country,HttpServletRequest a){

        int j=0;
        int sum=0;
        RegionStats[] rs = new RegionStats[270];
        RegionStats[] to = new RegionStats[12];

        String[] cty = new String[12];
        int[] tol = new int[12];
        int[] dif = new int[12];
        for(int i=0;i<270;i++)
        {
            rs[i] = new RegionStats();
        }
        for(int i=0;i<12;i++)
        {
            to[i] = new RegionStats();
        }
        List<RegionStats> allStats;
        List<RegionStats> all;

        all = dataServices.getAllRegionStats();
        if(country == null || country.equals("all") || country == "")
            allStats = dataServices.getAllRegionStats();
        else
            allStats = dataServices.getAllRegionStats()
                    .parallelStream()
                    .filter(locationStats -> locationStats.getCountry().equals(country))
                    .collect(Collectors.toUnmodifiableList());


        if(country == null || country.equals("all")) {
            model.addAttribute("country", "全球");
        }else {
            model.addAttribute("country", country);
        }
        model.addAttribute("localDate", LocalDate.now());

        int totalReportedCases = allStats.parallelStream().mapToInt(RegionStats::getLatestTotalCases).sum();
        int totalNewCases = allStats.parallelStream().mapToInt(RegionStats::getDiffFromPrevDay).sum();

        model.addAttribute("regionStats",allStats);
        model.addAttribute("totalReportedCases",totalReportedCases);
        model.addAttribute("totalNewCases",totalNewCases);

        for(int i=0;i<all.size();i++)
        {
            if(i == 0)
            {
                if(all.get(i).getCountry().equals(all.get(i+1).getCountry()))
                {
                    rs[j].setCountry(all.get(i).getCountry());
                    sum += all.get(i).getLatestTotalCases();
                    rs[j].setLatestTotalCases(sum);
                    rs[j].setDiffFromPrevDay(all.get(i).getDiffFromPrevDay());
                }
                else {
                    j++;
                    rs[j].setCountry(all.get(i).getCountry());
                    rs[j].setLatestTotalCases(all.get(i).getLatestTotalCases());
                    rs[j].setDiffFromPrevDay(all.get(i).getDiffFromPrevDay());
                    sum = 0;
                }
            }
            else if(i == all.size()-1)
            {
                if(all.get(i).getCountry().equals(all.get(i-1).getCountry()))
                {
                    rs[j].setCountry(all.get(i).getCountry());
                    sum += all.get(i).getLatestTotalCases();
                    rs[j].setLatestTotalCases(sum);
                    rs[j].setDiffFromPrevDay(all.get(i).getDiffFromPrevDay());
                }
                else {
                    j++;
                    rs[j].setCountry(all.get(i).getCountry());
                    rs[j].setLatestTotalCases(all.get(i).getLatestTotalCases());
                    rs[j].setDiffFromPrevDay(all.get(i).getDiffFromPrevDay());
                    sum=0;
                }
            }
            else if(all.get(i).getCountry().equals(all.get(i+1).getCountry()) || all.get(i).getCountry().equals(all.get(i-1).getCountry()))
            {
                rs[j].setCountry(all.get(i).getCountry());
                sum += all.get(i).getLatestTotalCases();
                rs[j].setLatestTotalCases(sum);
                rs[j].setDiffFromPrevDay(all.get(i).getDiffFromPrevDay());
            }
            else {
                j++;
                rs[j].setCountry(all.get(i).getCountry());
                rs[j].setLatestTotalCases(all.get(i).getLatestTotalCases());
                rs[j].setDiffFromPrevDay(all.get(i).getDiffFromPrevDay());
                sum=0;
            }
        }

        for(int i=0;i<j;i++)
        {
            for(int k=i+1;k<j;k++)
            {
                if(rs[i].getLatestTotalCases()<rs[k].getLatestTotalCases())
                {
                    int temp = rs[i].getLatestTotalCases();
                    String tmp = rs[i].getCountry();
                    int tem = rs[i].getDiffFromPrevDay();
                    rs[i].setLatestTotalCases(rs[k].getLatestTotalCases());
                    rs[i].setCountry(rs[k].getCountry());
                    rs[i].setDiffFromPrevDay(rs[k].getDiffFromPrevDay());
                    rs[k].setLatestTotalCases(temp);
                    rs[k].setCountry(tmp);
                    rs[k].setDiffFromPrevDay(tem);
                }
            }
        }
        for(int i=0;i<12;i++)
        {
            cty[i] = rs[i].getCountry();
            tol[i] = rs[i].getLatestTotalCases();
            dif[i] = rs[i].getDiffFromPrevDay();
            to[i].setCountry(rs[i].getCountry());
            to[i].setLatestTotalCases(rs[i].getLatestTotalCases());
        }
//        String country1 = rs[0].getCountry();
//        int tol1 = rs[0].getLatestTotalCases();
//        model.addAttribute("country1",country1);

        model.addAttribute("to",to);
        model.addAttribute("cty",cty);
        model.addAttribute("tol",tol);
        model.addAttribute("dif",dif);
        return "index";
    }
}
